package com.lzy.cloud.sms.service.impl;

import com.github.pagehelper.PageHelper;
import com.lzy.cloud.sms.dtos.MsgCodeDto;
import com.lzy.cloud.sms.entity.MsgCode;
import com.lzy.cloud.sms.mapper.MsgCodeMapperExt;
import com.lzy.cloud.sms.service.MsgCodeService;
import com.lzy.framework.core.orm.db.Pager;
import com.lzy.framework.core.utils.DozerUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MsgCodeServiceImpl implements MsgCodeService {
    @Autowired
    private MsgCodeMapperExt msgCodeMapperExt;

    @Override
    public Long insert(MsgCodeDto msgCodeDto) {
        MsgCode msgCode = new MsgCode();
        BeanUtils.copyProperties(msgCodeDto, msgCode);
        Integer res = msgCodeMapperExt.insertSelective(msgCode);
        return res > 0 ? msgCode.getId() : null;
    }

    @Override
    public int update(MsgCodeDto msgCodeDto) {
        MsgCode msgCode = new MsgCode();
        BeanUtils.copyProperties(msgCodeDto, msgCode);
        return msgCodeMapperExt.updateByPrimaryKeySelective(msgCode);
    }

    @Override
    public Boolean delete(Long aLong) {
        return null;
    }

    @Override
    public Boolean remove(Long aLong) {
        return null;
    }

    @Override
    public MsgCodeDto findById(Long aLong) {
        return DozerUtils.map(msgCodeMapperExt.selectByPrimaryKey(aLong), MsgCodeDto.class);
    }

    @Override
    public List<MsgCodeDto> query(Pager pager, MsgCodeDto msgCodeDto) {
        if (pager != null) {
            PageHelper.startPage(pager.getPageNum(), pager.getPageSize());
        }
        return msgCodeMapperExt.query(msgCodeDto);
    }

    @Override
    public MsgCodeDto findByTargetAndType(String phone, Integer type) {
        MsgCodeDto msgCodeDto = new MsgCodeDto();
        msgCodeDto.setAuthTarget(phone);
        msgCodeDto.setAuthType(type);
        List<MsgCodeDto> list = msgCodeMapperExt.query(msgCodeDto);
        return list.isEmpty() ? null : list.get(0);
    }
}
